package com.hankai.controller.backend;
import com.google.common.collect.Maps;
import com.hankai.common.Const;
import com.hankai.common.ResponseCode;
import com.hankai.common.ServerResponse;
import com.hankai.pojo.Product;
import com.hankai.pojo.User;
import com.hankai.service.IFileService;
import com.hankai.service.IProductService;
import com.hankai.service.IUserService;
import com.hankai.util.PropertiesUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by hankai on 2017/6/7.
 */
@Controller
@RequestMapping("/manage/product/")
public class ProductManageController {

    @Resource
    private IUserService iUserService;
    @Resource
    private IProductService iProductService;
    @Resource
    private IFileService iFileService;
    /**
     * 添加商品
     * @param session
     * @param product
     * @return
     */
    @RequestMapping(value = "productSave.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse productSave(HttpSession session, Product product){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),"请登录");
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            return response;
        }
        return iProductService.saveOrUpdateProduct(product);
    }

    /**
     * 修改状态
     * @param session
     * @param productId
     * @param status
     * @return
     */
    @RequestMapping(value = "updateStatus.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateStatus(HttpSession session, Integer productId,Integer status){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),"请登录");
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            return response;
        }
        return iProductService.updateState(productId,status);
    }

    /**
     * 查找商品明细
     * @param session
     * @param productId
     * @return
     */
    @RequestMapping(value = "getDetail.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getDetail(HttpSession session, Integer productId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),"请登录");
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            return response;
        }
        return iProductService.manageProductDetail(productId);
    }

    /**
     * 获取所有商品列表
     * @param session
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "getProductList.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getProductList(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),"请登录");
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            return response;
        }
        return iProductService.getProductList(pageNum,pageSize);
    }

    /**
     * 根据条件查询商品列表
     * @param session
     * @param productName
     * @param productId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "searchProductList.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse searchProductList(HttpSession session,String productName,Integer productId, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),"请登录");
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            return response;
        }
        return iProductService.searchProductList(productName,productId,pageNum,pageSize);
    }

    /**
     * 文件图片上传
     * @param file
     * @param request
     * @return
     */
    @RequestMapping(value = "upload.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse upload(HttpSession session, MultipartFile file, HttpServletRequest request){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),"请登录");
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            return response;
        }
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = iFileService.upload(file,path);
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
        Map fileMap = Maps.newHashMap();
        fileMap.put("uri",targetFileName);
        fileMap.put("url",url);
        return ServerResponse.createBySuccess(fileMap);
    }
    /**
     * 附文本图片上传
     * @param file
     * @param request
     * @return
     */
    @RequestMapping(value = "richtextImgUpload.do",method = RequestMethod.POST)
    @ResponseBody
    public Map richtextImgUpload(HttpSession session, MultipartFile file, HttpServletRequest request, HttpServletResponse response1){
        Map resultMap = Maps.newHashMap();
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            resultMap.put("success",false);
            resultMap.put("msg","请登录");
            return resultMap;
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            resultMap.put("success",false);
            resultMap.put("msg","你不是管理员");
            return resultMap;
        }
        //富文本中对于返回值有自己的要求，我们使用simditor所以按照simditor要求进行返回
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = iFileService.upload(file,path);
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
        if(StringUtils.isBlank(targetFileName)){
            resultMap.put("success",false);
            resultMap.put("msg","上传失败");
            return resultMap;
        }
        resultMap.put("uri",targetFileName);
        resultMap.put("url",url);
        resultMap.put("msg","上传成功");
        response1.addHeader("Access-Control-Allow-Headers","X-File-Name");
        return resultMap;
    }
}